The math library provides a set of mathematical functions that allow performing various mathematical calculations. To use these functions, need to include the math.h header.
#include <stdio.h>
#include <math.h>
int main() {
double num = 16.0;
double angle = 30.0;
// Calculate the square root of num
double squareRoot = sqrt(num);
printf("Square root of %.2f: %.2f\n", num, squareRoot);
// Calculate the value of num raised to the power of 3
double power = pow(num, 3);
printf("%.2f raised to the power of 3: %.2f\n", num, power);
// Convert the angle from degrees to radians
double radians = angle * M_PI / 180.0;
// Calculate the sine, cosine, and tangent of the angle in radians
double sineValue = sin(radians);
double cosineValue = cos(radians);
double tangentValue = tan(radians);
printf("Angle: %.2f degrees\n", angle);
printf("Sine: %.2f\n", sineValue);
printf("Cosine: %.2f\n", cosineValue);
printf("Tangent: %.2f\n", tangentValue);
// Calculate the exponential value of num
double expValue = exp(num);
printf("Exponential value of %.2f: %.2f\n", num, expValue);
// Calculate the natural logarithm of num
double logValue = log(num);
printf("Natural logarithm of %.2f: %.2f\n", num, logValue);
// Calculate the base 10 logarithm of num
double log10Value = log10(num);
printf("Base 10 logarithm of %.2f: %.2f\n", num, log10Value);
return 0;
}
In this program, calculate various mathematical operations using the functions from the math library.
Square root of 16.00: 4.00
16.00 raised to the power of 3: 4096.00
Angle: 30.00 degrees
Sine: 0.50
Cosine: 0.87
Tangent: 0.58
Exponential value of 16.00: 8886110.52
Natural logarithm of 16.00: 2.77
Base 10 logarithm of 16.00: 1.20
In the output, the results of the mathematical calculations performed using the math library functions.
What library is used for math functions in C?
What function rounds a floating-point number to the nearest integer in C?
What function calculates the absolute value of a number in C?
What function calculates the square root of a number in C?
What function returns the smallest integer greater than or equal to a number in C?